export default {
  data() {
    return {
      filter: {
        pageSize: 10,
        pageNum: 1,
        data: {}
      },
      loading: false,
      total: 0,
      form: {},
      title: '',
      tableData: [],
      visible: false,
      mode: 'form',
      showTable: false,
      target: {},
      ids: []
    }
  },
  computed: {
    renderArray() {
      return this.mode === 'form' ? this.formArray : this.detailArray
    }
  },
  created() {
    (this.formArray || []).forEach(async item => {
      if (item.dict) {
        const filter = (this.filterArray || []).find(f => f.dict === item.dict)
        let res = await this.getSystemDict(item.dict)
        filter.options = item.options = res.data.map(item => {
          return {
            label: item.dictName,
            value: item.dictId
          }
        })
      }
    })
  },
  watch: {
    /**
     * @description 当表单关闭时 将表单数据进行重置
     * */
    visible(val) {
      if (val) return
      this.$refs.uFormRef && this.$refs.uFormRef.reset()
    }
  },
  methods: {
    onClose() {
      this.visible = false
      this.beforeClose && this.beforeClose()
    },
    /**
     * @function getList 分页请求
     * @description 再混入页面进行重写
     * */
    getList() {

    },
    /**
     * @function getList请求参数
     @return object getList请求参数对象
     * */
    getListRequestParams() {
      const params = {
        pageSize: this.filter.pageSize,
        pageNum: this.filter.pageNum,
        ...this.filter.data
      }
      if (this.filterArray) {
        this.filterArray.map(item => {
          // 判断哪些不是请求时需要的参数 一般daterang时间范围选择器参数都是不需要传给后端的，分成两个参数start和end
          if (item.noRequestParams) params[item.key] = null
        })
      }
      return params
    },
    /**
     * @function getInit 分页请求
     * @param request  分页请求函数
     * @param options  默认参数
     * */
    async getInit(request, options = {}) {
      let res = null
      if (request instanceof Function) {
        let params = this.getListRequestParams()
        // console.log(params, 'list請求參數')
        this.loading = true
        try {
          if (options instanceof Object) {
            res = await request({
              ...params,
              ...options
            })
          } else if (typeof options === 'string') {
            res = await request(options)
          }
          this.tableData = res.rows
          this.total = res.total
          return res
        } finally {
          this.loading = false
        }
      } else {
        res = request
      }
    },
    /**
     * @function getDetail 获取详情
     * @param title 弹窗标题
     * @param request 详情请求函数
     * @param options 请求参数
     * */
    async getDetail(title, request, options) {
      this.visible = true
      this.title = title
      await this.$nextTick()
      let res = await request(options)
      this.form = res.data
      this.formArray.forEach(item => {
        if (item.fileList && this.form[item.key]) {
          item.fileList = this.form[item.key].split(',').map((file, index) => {
            return {
              name: '附件' + (index + 1),
              url: file,
              src: file,
              key: item.key
            }
          })
        }
        if (item.inputType === 'range') {
          this.$set(this.form, item.key, [res.data[item.start], res.data[item.end]])
        }
      })
      return res.data
    },
    /**
     * @function removeTip 删除提示
     * @param cb 回调删除函数
     * @param tip 提示内容 默认值 “该项数据”
     * */
    async removeTip(cb, tip) {
      try {
        await this.$confirm(`是否删除${tip || '该项数据'}`, '提示')
        await cb()
        this.$message.success('操作成功')
        await this.getList()
      } catch (e) {
      }
    },
    /**
     * @function formSubmitFunConfig 表单提交接口相关配置
     * @return addFunc 添加请求
     * @return modifyFunc 编辑请求
     * @return pk  主键
     * @return options 补充请求参数
     * @return cbFunc 执行成功回调
     * @description 在混入页面进行重写 返回相应的对象
     * */
    formSubmitFunConfig() {
      return {
        addFunc: () => {
        },
        modifyFunc: () => {
        },
        pk: () => {
        },
        options: {},
        cbFunc: () => {
          this.visible = false
        }
      }
    },
    /**
     * @function onsubmit 表单提交
     * @param cb 回调防止按钮 多次点击
     * */
    async onsubmit(cb) {
      const form = JSON.parse(JSON.stringify(this.form))
      const { addFunc, modifyFunc, pk, options, cbFunc } = this.formSubmitFunConfig()
      try {
        if (!form[pk]) {
          await addFunc({ ...options })
        } else {
          await modifyFunc({ ...options })
        }
        this.$message.success('操作成功')
        this.visible = false
        if (cb instanceof Function) cb()
        cbFunc && cbFunc()
        await this.getList()

      } catch (e) {
        if (cb instanceof Function) cb()
      }
    },
    /**
     * @function selectLinkTable 开启组件弹窗
     * @param item  保存所选表单项 最终提取 组件名称
     * */
    selectLinkTable(item) {
      this.target = item
      this.showTable = true
    },
    /**
     * @function rowClick
     * */
    rowClick(row) {
      this.$set(this.form, this.target.key, row[this.target.key])
      this.$set(this.form, this.target.key.replace('Name', 'Id'), row[this.target.key.replace('Name', 'Id')])
      this.showTable = false
    }
  }
}
